package com.aaa.controller;


import com.aaa.entity.Dept;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.aaa.entity.Emp;
import com.aaa.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * 员工表(Emp)表控制层
 *
 * @author makejava
 * @since 2022-12-07 09:51:55
 */
@RestController
@RequestMapping("emp")
@Slf4j
public class EmpController {
    /**
     * 服务对象
     */
    @Resource
    private EmpService empService;

    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param emp  查询实体
     * @return 所有数据
     */
    @GetMapping
    public Result selectAll(Page<Emp> page, Emp emp) {
        return Result.success(this.empService.page(page, new QueryWrapper<>(emp)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @Cacheable(cacheNames = "redis::emp",key = "#id")
    public Result<Emp> selectOne(@PathVariable Serializable id) {
        return Result.success(this.empService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param emp 实体对象
     * @return 新增结果
     */
    @PostMapping
    public Result insert(@RequestBody Emp emp) {
        log.info("emp = {}",emp);
        return Result.success(this.empService.save(emp));
    }

    /**
     * 修改数据
     *
     * @param emp 实体对象
     * @return 修改结果
     */
    @PutMapping
    @CachePut(cacheNames = "redis::emp",key = "#emp.id")
    public Result update(@RequestBody Emp emp) {
        boolean object = this.empService.updateById(emp);
        return Result.success(object);
    }

    /**
     * 删除数据
     *
     * @param ids id
     * @return 删除结果
     */
    @DeleteMapping
    @CacheEvict(cacheNames = "redis::emp",key = "#ids")
    public Result delete(@RequestParam("ids") List<Long> ids) {
        return Result.success(this.empService.removeByIds(ids));
    }
}

